
import sys

import pygame
from pygame.locals import *
import guanqia
from game import Game


class Jiemian:
    def __init__(self):
        W = 1600
        H = 900
        pygame.init()
        try:
            guanqia.Jiemian.jiemian_music.stop()
        except:
            print("出错了")
        pygame.display.set_caption("零零守护者")

        try:
            chuangkou = open("data/窗口大小.txt", "r", encoding="utf-8")
            chuangkou_size = chuangkou.read()
            chuangkou.close()
            print(chuangkou_size)
            if str(chuangkou_size) == "全屏模式":
                window = pygame.display.set_mode((W, H), FULLSCREEN | HWSURFACE, 32)
            else:
                window = pygame.display.set_mode((W, H))
        except:
            window = pygame.display.set_mode((W, H))
        img = pygame.image.load("img/界面背景.png")  # 如果你要尝试，要么图片放在原位，要么改变路径
        pygame.mixer.music.load("wav/游戏界面.mp3")  # 替换为你的音乐文件路径
        pygame.mixer.music.play(-1)  # -1表示无限循环播放

        # 按钮字体
        button_font = pygame.font.Font('font/datouren.ttf', 50)
        button_font2 = pygame.font.Font('font/datouren.ttf', 20)
        # 开始按钮
        x, y = 650, 350
        width, height = 200, 80
        button_rect = pygame.Rect(x, y, width, height)
        button_color = (45, 102, 78)
        text_color = (0, 0, 0)
        button_text = button_font.render("开始", True, text_color)

        # 设置按钮
        x1, y1 = 650, 470
        width1, height1 = 200, 80
        button_rect1 = pygame.Rect(x1, y1, width1, height1)
        button_color1 = (45, 102, 78)
        text_color1 = (0, 0, 0)
        if Game.quanping:
            button_text1 = button_font.render("全屏", True, text_color1)
        else:
            button_text1 = button_font.render("窗口", True, text_color1)
        # 设置按钮
        x2, y2 = 650, 590
        width2, height2 = 200, 80
        button_rect2 = pygame.Rect(x2, y2, width2, height2)
        button_color2 = (45, 102, 78)
        text_color2 = (0, 0, 0)
        button_text2 = button_font.render("退出", True, text_color2)

        # 音量按钮
        x3, y3 = 0, 0
        width3, height3 = 50, 50
        button_rect3 = pygame.Rect(x3, y3, width3, height3)
        button_color3 = (45, 102, 78)
        text_color3 = (0, 0, 0)
        button_text3 = button_font2.render("静音", True, text_color3)

        # 加载按钮声音
        button_sound = pygame.mixer.Sound("wav/跳.wav")  # 替换为你的音乐文件路径

        click_button = 0
        # 定义按钮状态
        button_pressed = False
        button_pressed1 = False
        button_pressed2 = False
        button_pressed3 = False
        Fullscreen = False
        Game.flag = True
        while Game.flag:
            # 背景图刷新
            window.blit(img, (0, 1))
            # 绘制按钮
            # 开始按钮
            pygame.draw.rect(window, button_color, button_rect)
            window.blit(button_text, (button_rect.centerx - button_text.get_width()
                                      / 2, button_rect.centery - button_text.get_height() / 2))

            # 设置按钮
            pygame.draw.rect(window, button_color1, button_rect1)
            window.blit(button_text1, (button_rect1.centerx - button_text1.get_width()
                                       / 2, button_rect1.centery - button_text1.get_height() / 2))
            # 退出按钮
            pygame.draw.rect(window, button_color2, button_rect2)
            window.blit(button_text2, (button_rect2.centerx - button_text2.get_width()
                                       / 2, button_rect2.centery - button_text2.get_height() / 2))

            # 音量按钮
            pygame.draw.rect(window, button_color3, button_rect3)
            window.blit(button_text3, (button_rect3.centerx - button_text3.get_width()
                                       / 2, button_rect3.centery - button_text3.get_height() / 2))

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    Game.flag = False
                    sys.exit()

                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_p:
                        Fullscreen = not Fullscreen
                        if Fullscreen:
                            window = pygame.display.set_mode((W, H), FULLSCREEN | HWSURFACE, 32)
                        else:
                            window = pygame.display.set_mode((W, H), 0, 32)

                # 点击按钮
                if event.type == pygame.MOUSEBUTTONDOWN:
                    if button_rect.collidepoint(event.pos):
                        # 在这里执行按钮点击后的操作
                        print("开始按钮")
                        button_color = (255, 255, 0)
                        button_pressed = True
                        button_sound.play()
                        guanqia.Jiemian()

                if event.type == pygame.MOUSEBUTTONDOWN:
                    if button_rect1.collidepoint(event.pos):
                        # 在这里执行按钮点击后的操作
                        print("设置按钮")
                        chuangkou = open("data/窗口大小.txt", "w", encoding="utf-8")
                        if Game.quanping:
                            Game.window = pygame.display.set_mode((Game.W, Game.H), FULLSCREEN | HWSURFACE, 32)
                            chuangkou.write("全屏模式")
                            button_text1 = button_font.render("窗口", True, text_color1)
                            chuangkou.close()
                            Game.quanping = False
                        else:
                            Game.window = pygame.display.set_mode((Game.W, Game.H))
                            Game.quanping = True
                            chuangkou.write("窗口模式")
                            button_text1 = button_font.render("全屏", True, text_color1)
                            chuangkou.close()
                        button_color1 = (255, 255, 0)
                        button_pressed1 = True
                        button_sound.play()

                if event.type == pygame.MOUSEBUTTONDOWN:
                    if button_rect2.collidepoint(event.pos):
                        # 在这里执行按钮点击后的操作
                        print("设置按钮")
                        button_color2 = (255, 255, 0)
                        button_pressed2 = True
                        button_sound.play()

                if event.type == pygame.MOUSEBUTTONDOWN:
                    if button_rect3.collidepoint(event.pos):
                        # 在这里执行按钮点击后的操作
                        print("音量按钮")
                        button_color3 = (255, 255, 0)
                        click_button += 1
                        if click_button % 2 == 1:
                            button_text3 = button_font2.render("播放", True, text_color3)
                            # 暂停音乐
                            pygame.mixer.music.pause()
                        if click_button % 2 == 0:
                            button_text3 = button_font2.render("暂停", True, text_color3)
                            pygame.mixer.music.unpause()
                        button_pressed3 = True

                # 处理鼠标松开事件
                if event.type == pygame.MOUSEBUTTONUP:
                    if button_pressed:
                        button_color = (45, 102, 78)
                        button_pressed = False
                        print("开始按钮松开了")

                if event.type == pygame.MOUSEBUTTONUP:
                    if button_pressed1:
                        button_color1 = (45, 102, 78)
                        button_pressed1 = False
                        print("设置按钮")

                if event.type == pygame.MOUSEBUTTONUP:
                    if button_pressed2:
                        sys.exit()

                if event.type == pygame.MOUSEBUTTONUP:
                    if button_pressed3:
                        button_color3 = (45, 102, 78)
                        button_pressed3 = False
                        print("音量松开了")

            pygame.display.flip()  # 更新屏幕显示


if __name__ == '__main__':
    Jiemian()
